# -*- coding: utf-8 -*-
"""
-------------------------------------------------
   File Name：     RedisClient
   Description :   代理ip检查
   Author :        William
   date：          2020.2.19
-------------------------------------------------
   Change Activity:
                   2020.2.19:  代理ip检查
-------------------------------------------------
"""
__author__ = 'William'

import json
import time
import requests
import traceback
from Database.RedisClient import Redis_cliient
from Encryption.python_aes import aes
from concurrent.futures import ThreadPoolExecutor
from Config.Setting import logger

def check_ip(data):
    data = json.loads(data)
    proxy_ip = data["proxy_ip"]
    try:
        type = data["type"]
        if type == 1:
            user = data["user"]
            password = aes.decrypt((data["password"]).encode(encoding='utf-8'))
            proxies = {
                "http": f"http://{user}:{password}@{proxy_ip}",
                "https": f"https://{user}:{password}@{proxy_ip}",
            }
        else:
            proxies = {
                "http": f"http://{proxy_ip}",
                "https": f"https://{proxy_ip}",
            }
        response = requests.get("https://www.baidu.com",timeout=3,proxies=proxies)
        if response.status_code == 200:
            # logger.info("ip可用")
            pass
        else:
            Redis_cliient.delete(proxy_ip)
    except:
        Redis_cliient.delete(proxy_ip)
        # traceback.print_exc()

def check_ip_main():
    logger.info("代理检查任务调度开始！")
    while True:
        with ThreadPoolExecutor(max_workers=20) as t:
            try:
                proxy_list = Redis_cliient.getAll()
                for x in proxy_list:
                    t.submit(check_ip,x)
                logger.info("检查完成！！")
            except:
                traceback.print_exc()
        time.sleep(60)

if __name__ == '__main__':
    s = time.time()
    check_ip_main()
    e = time.time()
    print(e-s)